/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package LDAPManager;

import LDAPAPI.AnvilLDAP;
import LDAPAPI.Exceptions.LDAPActionException;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author anvil
 */
@WebServlet(name = "AddEntry", urlPatterns = {"/AddEntry"})
public class AddEntry extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        RequestDispatcher rd;

        HttpSession session = request.getSession();

        AnvilLDAP ldap = (AnvilLDAP) session.getAttribute("ldap");

        if (ldap != null) {
            String entryType = request.getParameter("entryType");
            if (entryType != null) {
                String form_rootdn;
                
                switch (entryType) {
                    case "":
                        request.setAttribute("error", "Invalid Form. Use a different form type.");
                        rd = request.getServletContext().getRequestDispatcher("/AddEntry.jsp");
                        rd.forward(request, response);
                        break;
                    case "user":
                        String form_user_naming = request.getParameter("form_user_naming");
                        form_rootdn = request.getParameter("form_rootdn");
                        String form_user_firstname = request.getParameter("form_user_firstname");
                        String form_user_lastname = request.getParameter("form_user_lastname");
                        String form_user_commonname = request.getParameter("form_user_commonname");
                        String form_user_userid = request.getParameter("form_user_userid");
                        String form_user_password = request.getParameter("form_user_password");
                        String form_user_email = request.getParameter("form_user_email");
                        String form_user_telephone = request.getParameter("form_user_telephone");

                        if (form_user_naming != null && !form_user_naming.equals("")
                                && form_rootdn != null && !form_rootdn.equals("")
                                && form_user_firstname != null && !form_user_firstname.equals("")
                                && form_user_lastname != null && !form_user_lastname.equals("")
                                && form_user_commonname != null && !form_user_commonname.equals("")
                                && form_user_userid != null && !form_user_userid.equals("")
                                && form_user_password != null && !form_user_password.equals("")
                                && form_user_email != null && !form_user_email.equals("")
                                && form_user_telephone != null && !form_user_telephone.equals("")) {
                            try {
                                ldap.addUser(form_user_naming, form_rootdn, form_user_firstname, form_user_lastname, form_user_commonname, form_user_userid, form_user_password, form_user_email, form_user_telephone);

                                rd = request.getServletContext().getRequestDispatcher("/LDAPManager.jsp");
                                rd.forward(request, response);
                            } catch (LDAPActionException e) {
                                request.setAttribute("error", "Problem while creating Entry. Plase verify that all data is correct.");
                                rd = request.getServletContext().getRequestDispatcher("/AddEntry.jsp");
                                rd.forward(request, response);
                            }
                        } else {
                            request.setAttribute("error", "Not all data was filled in the form. Plase re-enter data.");
                            rd = request.getServletContext().getRequestDispatcher("/AddEntry.jsp");
                            rd.forward(request, response);
                        }

                        break;
                    case "group":
                        form_rootdn = request.getParameter("form_rootdn");
                        String form_group_name = request.getParameter("form_group_name");
                        String form_group_description = request.getParameter("form_group_description");
                        String form_group_members = request.getParameter("form_group_members");

                        if (form_group_name != null && !form_group_name.equals("")
                                && form_rootdn != null && !form_rootdn.equals("")
                                && form_group_description != null && !form_group_description.equals("")
                                && form_group_members != null && !form_group_members.equals("")) {
                            try {
                                ldap.addGroup(form_rootdn, form_group_name, form_group_description, form_group_members);

                                rd = request.getServletContext().getRequestDispatcher("/LDAPManager.jsp");
                                rd.forward(request, response);
                            } catch (LDAPActionException e) {
                                request.setAttribute("error", "Problem while creating Entry. Plase verify that all data is correct.");
                                rd = request.getServletContext().getRequestDispatcher("/AddEntry.jsp");
                                rd.forward(request, response);
                            }
                        } else {
                            request.setAttribute("error", "Not all data was filled in the form. Plase re-enter data.");
                            rd = request.getServletContext().getRequestDispatcher("/AddEntry.jsp");
                            rd.forward(request, response);
                        }

                        break;
                    case "ou":
                        form_rootdn = request.getParameter("form_rootdn");
                        String form_ou_name = request.getParameter("form_ou_name");
                        String form_ou_description = request.getParameter("form_ou_description");
                        String form_ou_address = request.getParameter("form_ou_address");
                        String form_ou_telephone = request.getParameter("form_ou_telephone");

                        if (form_ou_name != null && !form_ou_name.equals("")
                                && form_rootdn != null && !form_rootdn.equals("")
                                && form_ou_description != null && !form_ou_description.equals("")
                                && form_ou_address != null && !form_ou_address.equals("")
                                && form_ou_telephone != null && !form_ou_telephone.equals("")) {
                            try {
                                ldap.addOrganizationalUnit(form_rootdn, form_ou_name, form_ou_description, form_ou_address, form_ou_telephone);

                                rd = request.getServletContext().getRequestDispatcher("/LDAPManager.jsp");
                                rd.forward(request, response);
                            } catch (LDAPActionException e) {
                                request.setAttribute("error", "Problem while creating Entry. Plase verify that all data is correct.");
                                rd = request.getServletContext().getRequestDispatcher("/AddEntry.jsp");
                                rd.forward(request, response);
                            }
                        } else {
                            request.setAttribute("error", "Not all data was filled in the form. Plase re-enter data.");
                            rd = request.getServletContext().getRequestDispatcher("/AddEntry.jsp");
                            rd.forward(request, response);
                        }

                        break;
                    case "org":
                        form_rootdn = request.getParameter("form_rootdn");
                        String form_o_name = request.getParameter("form_o_name");
                        String form_o_description = request.getParameter("form_o_description");

                        if (form_o_name != null && !form_o_name.equals("")
                                && form_rootdn != null && !form_rootdn.equals("")
                                && form_o_description != null && !form_o_description.equals("")) {
                            try {
                                ldap.addOrganization(form_rootdn, form_o_name, form_o_description);

                                rd = request.getServletContext().getRequestDispatcher("/LDAPManager.jsp");
                                rd.forward(request, response);
                            } catch (LDAPActionException e) {
                                request.setAttribute("error", "Problem while creating Entry. Plase verify that all data is correct.");
                                rd = request.getServletContext().getRequestDispatcher("/AddEntry.jsp");
                                rd.forward(request, response);
                            }
                        } else {
                            request.setAttribute("error", "Not all data was filled in the form. Plase re-enter data.");
                            rd = request.getServletContext().getRequestDispatcher("/AddEntry.jsp");
                            rd.forward(request, response);
                        }

                        break;
                    case "domain":
                        form_rootdn = request.getParameter("form_rootdn");
                        String form_domain_name = request.getParameter("form_domain_name");
                        String form_domain_description = request.getParameter("form_domain_description");

                        if (form_domain_name != null && !form_domain_name.equals("")
                                && form_rootdn != null && !form_rootdn.equals("")
                                && form_domain_description != null && !form_domain_description.equals("")) {
                            try {
                                ldap.addDomain(form_rootdn, form_domain_name, form_domain_description);

                                rd = request.getServletContext().getRequestDispatcher("/LDAPManager.jsp");
                                rd.forward(request, response);
                            } catch (LDAPActionException e) {
                                request.setAttribute("error", "Problem while creating Entry. Plase verify that all data is correct.");
                                rd = request.getServletContext().getRequestDispatcher("/AddEntry.jsp");
                                rd.forward(request, response);
                            }
                        } else {
                            request.setAttribute("error", "Not all data was filled in the form. Plase re-enter data.");
                            rd = request.getServletContext().getRequestDispatcher("/AddEntry.jsp");
                            rd.forward(request, response);
                        }

                        break;
                }
            } else {
                request.setAttribute("error", "Invalid Entry Type submitted.");
                rd = request.getServletContext().getRequestDispatcher("/AddEntry.jsp");
                rd.forward(request, response);
            }
        } else {
            request.setAttribute("error", "Session Invalid.");
            rd = request.getServletContext().getRequestDispatcher("/index.jsp");
            rd.forward(request, response);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
